{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.35714285714285715\n"
     ]
    },
    {
     "data": {
      "text/plain": "0.9402859586706309"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "def Information_gain(x, y):\n",
    "    px = float(x / (x+y))\n",
    "    print(px)\n",
    "    py = float(y / (x+y))\n",
    "    E = -(px * math.log(px,2)) - (py * math.log(py,2))\n",
    "    return E\n",
    "a = Information_gain(5, 9)\n",
    "a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "['no',\n 'no',\n 'yes',\n 'yes',\n 'yes',\n 'no',\n 'yes',\n 'no',\n 'yes',\n 'yes',\n 'yes',\n 'yes',\n 'yes',\n 'no']"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('./example_data.csv')\n",
    "df['play'].tolist()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "0.9402859586706309"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算熵\n",
    "def entropy(ele):\n",
    "    probs = [ele.count(i)/len(ele) for i in set(ele)]\n",
    "    entropy = -sum([prob*math.log(prob, 2) for prob in probs])\n",
    "    return entropy\n",
    "entropy(df['play'].tolist())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "{'hot':    humility   outlook play temp  windy\n 0      high     sunny   no  hot  False\n 1      high     sunny   no  hot   True\n 2      high  overcast  yes  hot  False\n 12   normal  overcast  yes  hot  False,\n 'mild':    humility   outlook play  temp  windy\n 3      high     rainy  yes  mild  False\n 7      high     sunny   no  mild  False\n 9    normal     rainy  yes  mild  False\n 10   normal     sunny  yes  mild   True\n 11     high  overcast  yes  mild   True\n 13     high     rainy   no  mild   True,\n 'cool':   humility   outlook play  temp  windy\n 4   normal     rainy  yes  cool  False\n 5   normal     rainy   no  cool   True\n 6   normal  overcast  yes  cool   True\n 8   normal     sunny  yes  cool  False}"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def split_dataframe(data, col):\n",
    "    unique_values = data[col].unique()  # 去除重复的字段并转换为列表\n",
    "    # print(unique_values)\n",
    "    result_dict = {ele:pd.DataFrame for ele in unique_values}\n",
    "    # print(result_dict)\n",
    "    for key in result_dict.keys():\n",
    "        result_dict[key] = data[:][data[col] == key]\n",
    "    return result_dict\n",
    "split_example = split_dataframe(df, 'temp')\n",
    "split_example"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "def choose_best_col(data, label):\n",
    "    entropy_D = entropy(df[label].tolist())\n",
    "    #  获取列名除了label\n",
    "    cols = [col for col in df.columns if col not in [label]]\n",
    "    # print(cols)\n",
    "    max_value, best_col = -999, None\n",
    "    max_split = None\n",
    "    for col in cols:\n",
    "        splited_set = split_dataframe(df, col)\n",
    "        entropy_DA = 0\n",
    "        for subset_col, subset in splited_set.items():\n",
    "            entropy_Di = entropy(subset[label].tolist())\n",
    "            entropy_DA += len(subset)/len(df) * entropy_Di\n",
    "        # 计算信息增益\n",
    "        info_gain = entropy_D - entropy_DA\n",
    "        if info_gain > max_value:\n",
    "            max_value, best_col = info_gain, col\n",
    "            max_splited = splited_set\n",
    "    return max_value, best_col, max_splited\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['humility', 'outlook', 'temp', 'windy']\n"
     ]
    },
    {
     "data": {
      "text/plain": "(0.2467498197744391,\n 'outlook',\n {'sunny':    humility outlook play  temp  windy\n  0      high   sunny   no   hot  False\n  1      high   sunny   no   hot   True\n  7      high   sunny   no  mild  False\n  8    normal   sunny  yes  cool  False\n  10   normal   sunny  yes  mild   True,\n  'overcast':    humility   outlook play  temp  windy\n  2      high  overcast  yes   hot  False\n  6    normal  overcast  yes  cool   True\n  11     high  overcast  yes  mild   True\n  12   normal  overcast  yes   hot  False,\n  'rainy':    humility outlook play  temp  windy\n  3      high   rainy  yes  mild  False\n  4    normal   rainy  yes  cool  False\n  5    normal   rainy   no  cool   True\n  9    normal   rainy  yes  mild  False\n  13     high   rainy   no  mild   True})"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choose_best_col(df, 'play')\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}